package com.nest.WorkNest.act;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RecommendAct extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.edtTxt_recommend_percentage)
    EditText edtTxtRecommendPercentage;
    @Bind(R.id.edtTxt_recommend_describe)
    EditText edtTxtRecommendDescribe;
    @Bind(R.id.btn_recommend_submit)
    Button btnRecommendSubmit;
    private Context context = RecommendAct.this;
    private String productId, percentage, describe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recommend);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        initBar();
        actionbar_side_name.setVisibility(View.VISIBLE);
        actionbar_side_name.setText("申请推款");
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        back();
        btnRecommendSubmit.setOnClickListener(this);
    }

    public void loadApply() {
        productId = getIntent().getStringExtra("productId");
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                Logger.json(result);
                if (Tool.getString(result, "resultCode").equals("0")) {
                    showToast("提交成功，请等待审核");
                    finish();
                } else {
                    showToast(Tool.getString(result, "resultCode"));
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostApply(context, "1", percentage, describe, productId, ioAuthCallBack);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                    manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recommend_submit:
                percentage = edtTxtRecommendPercentage.getText().toString();
                describe = edtTxtRecommendDescribe.getText().toString();
                if (TextUtils.isEmpty(percentage) && TextUtils.isEmpty(describe)) {
                    showToast("佣金比例与推款说明不能为空");
                } else {
                    loadApply();
                }
                break;
        }
    }
}
